/**
 * n² + an + b, where |a| < 1000 and |b| < 1000
 * that produces the maximum number of primes 
 * for consecutive values of n, starting with n = 0.
 * Update 1:  Reduce from 4 mins to 6 seconds :lol:
 * Well, lazy mind
 * 1. B is prime that should be bigger than 41
 * 2. A should be odd.
 * Update 2: remove the abs(b)
 * Update 3: Remove the e.printStackTrace() reduce from 6 secs to 110 ms :lol:
 * Update 4: Change the loop position b then a, in b using b=b+2, in n no need using abs
 * Update 5: Check the value calculation before checking prime, reduce from 100 to 33 ms :v 
 */

/**
 * @author TrinhNX
 * @start_date	: 2015/04/21
 * @end_date 	:
 */
public class Euler027 {

	public static void main(String[] args) {

		final long start = System.currentTimeMillis();
		final int A = 1000;
		final int B = 1000;
		// Of course n < B
		// We looopppppppppp
		int counter = 0;
		int temp_a = 0, temp_b = 0;
		for (int b = 41; b < B; b = b + 2) {
			if (Common.isPrime(b)) {
				for (int a = -A + 1; a < A; a++) {
					if (a % 2 != 0) {
						int temp = 0;
						int val;
						for (int n = 0; n < b; n++) {
							// If is prime
							val = calFormula(n, a, b);
							if (val < 2) {
								break;
							}
							if (Common.isPrime(val)) {
								temp++;
							} else {
								break;
							}
						}
						if (temp > counter) {
							temp_a = a;
							temp_b = b;
							counter = temp;
						}
					}
				}
			}
		}
		System.out.println("A: " + temp_a + "\tB: " + temp_b + "\tValue: " + counter);
		System.out.println("End after: " + (System.currentTimeMillis() - start));
	}

	/**
	 * @author		: TrinhNX
	 * @param x
	 * @param a
	 * @param b
	 * @return
	 * @start_date	: 2015/04/21
	 * @end_date	:
	 */
	private static int calFormula(final int x, final int a, final int b) {
		return (x * x + a * x + b);
	}

}
